public class Solution856 {

    int cur=0;

    public int scoreOfParentheses(String s) {
        if (s.charAt(cur)==')'){
            cur++;
            return 1;
        }
        int t=0;
        while (cur<s.length() && s.charAt(cur)!=')'){
            if (s.charAt(cur)=='('){
                cur++;
                int last=cur;
                int m=scoreOfParentheses(s);
                if (last+1==cur){
                    t+=1;
                }else {
                    t+=2*m;
                }
            }
        }
        cur++;
        return t;
    }

    public static void main(String[] args) {
        System.out.println(new Solution856().scoreOfParentheses("(()(()))"));
    }
}
